package com.xdja.myapplication.helper

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.ViewAnimationUtils
import android.view.animation.BounceInterpolator
import androidx.constraintlayout.widget.ConstraintHelper
import androidx.constraintlayout.widget.ConstraintLayout

/**
 * @program: My Application
 *
 * @description:
 *
 * @author: flyou
 *
 * @email: fzl@xdja.com
 *
 * @create: 2019-09-23 09:08
 **/
class CircularRevealHelper @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintHelper(context, attrs, defStyleAttr) {

    var firstLoad=true
    override fun updatePostLayout(container: ConstraintLayout) {
        super.updatePostLayout(container)
        if (!firstLoad){
            return
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if (this.mReferenceIds != null) {
                this.setIds(this.mReferenceIds)
            }
            val views = getViews(container)
            for (view in views) {
                val anim = ViewAnimationUtils.createCircularReveal(
                    view, view.width / 2,
                    view.height / 2, 0f,
                    Math.hypot((view.height / 2).toDouble(), (view.width / 2).toDouble()).toFloat()
                )
                anim.duration = 2000
                anim.start()
            }
            firstLoad=false
        }
    }
}


class VisibleHelper @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintHelper(context, attrs, defStyleAttr) {


    override fun updatePreLayout(container: ConstraintLayout?) {
        super.updatePreLayout(container)
        var visible = visibility;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if (this.mReferenceIds != null) {
                this.setIds(this.mReferenceIds)
            }
            val views = getViews(container)

            for (view in views) {
                view.visibility = visible
            }

        }
    }
}


class EnterHelper @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintHelper(context, attrs, defStyleAttr) {
    var firstLoad=true


    override fun updatePostLayout(container: ConstraintLayout?) {
        super.updatePostLayout(container)
        if (!this.firstLoad) return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if (this.mReferenceIds != null) {
                this.setIds(this.mReferenceIds)
            }
            val views = getViews(container)

            for (view in views) {
                var set =AnimatorSet()
                var alphaAnimator: ObjectAnimator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)
                var scaleXAnimator:ObjectAnimator= ObjectAnimator.ofFloat(view,"scaleX",0f,1f)
                var scaleYAnimator:ObjectAnimator= ObjectAnimator.ofFloat(view,"scaleY",0f,1f)
                set.playTogether(alphaAnimator,scaleXAnimator,scaleYAnimator)
                set.duration=2000
                set.start()

            }
            firstLoad=false
        }
    }
}

class FlyingBounceHelper : ConstraintHelper {
    private  var mContainer: ConstraintLayout?=null
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )
    override fun updatePreLayout(container: ConstraintLayout) {

        if (mContainer !=container) {
            if (this.mReferenceIds != null) {
                this.setIds(this.mReferenceIds)
            }
            val views = getViews(container)
            for (i in 0 until mCount) {
                val view = views[i]
                val animator =
                    ObjectAnimator.
                        ofFloat(view, "translationX", -2000f, 0f)
                        .setDuration(2000)
                animator.interpolator = BounceInterpolator()
                animator.start()
            }
        }
        mContainer = container
    }
}
